﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Security.Cryptography;



namespace Talk
{
    public partial class admin : System.Web.UI.Page
    {
        bool IsAdmin()
        {
            if (Session["UserID"] == null) return false;
            if (Session["Level"] != null && Session["Level"].ToString() == "9")
                return true;
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["talkconn"].ConnectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select Level from Administrators where UserID = '"
                    + Session["UserID"].ToString() + "'";
                SqlDataReader data = cmd.ExecuteReader();
                if(data.HasRows)
                {
                    data.Read();
                    Session["Level"] = data["Level"];
                    if (Session["Level"].ToString() == "9") return true;
                }
                data.Close();
                conn.Close();
            }
            catch (System.Exception ex)
            {
                errormsg.Text = ex.Message;
            }
            return false;

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsAdmin())
            {
                Response.Redirect("/index.aspx");
                return;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!IsAdmin())
            {
                Response.Redirect("/index.aspx");
                return;
            }
            content.Rows.Clear();
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["talkconn"].ConnectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "exec mostpro";
                SqlDataReader data = cmd.ExecuteReader();
                while(data.Read())
                {
                    TableRow tr = new TableRow();
                    TableCell td1 = new TableCell();
                    td1.Text = data["UserID"].ToString();
                    tr.Cells.Add(td1);
                    TableCell td2 = new TableCell();
                    td2.Text = data["Username"].ToString();
                    tr.Cells.Add(td2);
                    TableCell td3 = new TableCell();
                    td3.Text = data["Rate"].ToString();
                    tr.Cells.Add(td3);
                    content.Rows.Add(tr);
                }
                data.Close();
                conn.Close();
            }
            catch (System.Exception ex)
            {
                errormsg.Text = ex.Message;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (!IsAdmin())
            {
                Response.Redirect("/index.aspx");
                return;
            }
            content.Rows.Clear();
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["talkconn"].ConnectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "exec mostcon";
                SqlDataReader data = cmd.ExecuteReader();
                while (data.Read())
                {
                    TableRow tr = new TableRow();
                    TableCell td1 = new TableCell();
                    td1.Text = data["UserID"].ToString();
                    tr.Cells.Add(td1);
                    TableCell td2 = new TableCell();
                    td2.Text = data["Username"].ToString();
                    tr.Cells.Add(td2);
                    TableCell td3 = new TableCell();
                    td3.Text = data["Rate"].ToString();
                    tr.Cells.Add(td3);
                    content.Rows.Add(tr);
                }
                data.Close();
                conn.Close();
            }
            catch (System.Exception ex)
            {
                errormsg.Text = ex.Message;
            }
        }
    }
}
